The filter function filters an iterable to what specific data you want. Here's the syntax:
filter(function, iterable)
As you would expect like in map() and reduce(), we are going to use lambda expressions for the most part using filter. In Python 3, filter returns a filter object. You need to cast it to a list.
Example 1 Words that start with 'M'
In [17]:
words = ["moo", "noon", "height", "malicious", "regex"]
m_words = filter(lambda word: word if word[0].lower() == 'm' else None, words)
print(list(m_words))
Example 2 The mandatory even number example
In [19]:
numbers = [3, 6, 38, 57, 37, 58, 25 , 47, 56, 3 , 8 , 9]
even = filter(lambda num: num % 2 == 0, numbers)
print(list(even))